package com.gxp.framework.shiro.audit;

import com.gxp.common.constant.Constants;
import com.gxp.common.enums.BusinessAction;
import com.gxp.common.utils.StringUtils;
import com.gxp.framework.manager.AsyncManager;
import com.gxp.framework.manager.factory.AsyncFactory;
import org.springframework.stereotype.Component;

/**
 * 审计功能服务层
 */
@Component
public class AuditService {

    /** 登录相关审计记录 */
    public void recordAuditLoginSuccess(String what, String username, String role, String message) {
        AsyncManager.me().execute(AsyncFactory.recordAudit("登录", BusinessAction.SIGNEDIN.ordinal(), what + ": " + username, username, role, Constants.SUCCESS, message, ""));
    }

    public void recordAuditLoginFail(String what, String username, String message) {
        AsyncManager.me().execute(AsyncFactory.recordAudit("登录", BusinessAction.SIGNEDIN.ordinal(), what + ": " + username, username, "-", Constants.FAIL, message, ""));
    }

    /** 审计追踪记录 */
    public void recordAuditSuccess(String title, int type, String what, String params){
        AsyncManager.me().execute(AsyncFactory.recordAuditSuccess(title, type, what, params));
    }

    public void recordAuditFail(String title, int type, String what, String params, String errorMsg){
        AsyncManager.me().execute(AsyncFactory.recordAuditFail(title, type, what, errorMsg, params));
    }

    /**
     * SpinStudioJ同步AuditLog到服务器（）
     * @param infos
     */
    public void recordAuditLines(String infos, String who){
        String[] split = infos.split("\r\n");
        for (String line: split){
            if(StringUtils.isEmpty(line)){
                continue;
            }
            //格式：2024-10-21 15:38:24.855 - Login in :admin  time - what

        }
    }

}
